<template>
  <div class="formal-appointment">
    <div class="header">
      <router-link to="/main/renshi/zhaoping/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>正式任职</h2>
    </div>

    <div class="content">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="待转正" name="pending">
          <el-table :data="pendingList" style="width: 100%">
            <el-table-column prop="name" label="姓名" width="120"></el-table-column>
            <el-table-column prop="position" label="职位" width="150"></el-table-column>
            <el-table-column prop="department" label="部门" width="120"></el-table-column>
            <el-table-column prop="entryDate" label="入职日期" width="180"></el-table-column>
            <el-table-column prop="probationResult" label="试用期考核" width="120">
              <template slot-scope="scope">
                <el-tag type="success">{{ scope.row.probationResult }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" @click="handleFormal(scope.row)">转正</el-button>
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="已转正" name="completed">
          <el-table :data="completedList" style="width: 100%">
            <el-table-column prop="name" label="姓名" width="120"></el-table-column>
            <el-table-column prop="position" label="职位" width="150"></el-table-column>
            <el-table-column prop="department" label="部门" width="120"></el-table-column>
            <el-table-column prop="formalDate" label="转正日期" width="180"></el-table-column>
            <el-table-column prop="salary" label="转正薪资" width="120"></el-table-column>
            <el-table-column label="操作" width="120">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 转正对话框 -->
    <el-dialog title="员工转正" :visible.sync="formalDialogVisible" width="50%">
      <el-form :model="formalForm" label-width="120px" v-if="currentEmployee">
        <el-form-item label="基本信息">
          <el-descriptions :column="2" border size="small">
            <el-descriptions-item label="姓名">{{ currentEmployee.name }}</el-descriptions-item>
            <el-descriptions-item label="职位">{{ currentEmployee.position }}</el-descriptions-item>
            <el-descriptions-item label="部门">{{ currentEmployee.department }}</el-descriptions-item>
            <el-descriptions-item label="入职日期">{{ currentEmployee.entryDate }}</el-descriptions-item>
          </el-descriptions>
        </el-form-item>

        <el-form-item label="转正日期">
          <el-date-picker v-model="formalForm.formalDate" type="date" placeholder="选择转正日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="转正薪资">
          <el-input-number v-model="formalForm.salary" :min="0" :step="1000"></el-input-number>
        </el-form-item>

        <el-form-item label="岗位职级">
          <el-select v-model="formalForm.level">
            <el-option label="P1" value="P1"></el-option>
            <el-option label="P2" value="P2"></el-option>
            <el-option label="P3" value="P3"></el-option>
            <el-option label="P4" value="P4"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="转正评语">
          <el-input type="textarea" v-model="formalForm.comment" rows="4"></el-input>
        </el-form-item>

        <el-form-item label="工作目标">
          <el-input type="textarea" v-model="formalForm.goals" rows="4" 
            placeholder="设定未来工作目标和期望"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formalDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFormal">确认转正</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="转正详情" :visible.sync="detailDialogVisible" width="50%">
      <div class="formal-detail" v-if="currentEmployee">
        <el-descriptions title="基本信息" :column="2" border>
          <el-descriptions-item label="姓名">{{ currentEmployee.name }}</el-descriptions-item>
          <el-descriptions-item label="职位">{{ currentEmployee.position }}</el-descriptions-item>
          <el-descriptions-item label="部门">{{ currentEmployee.department }}</el-descriptions-item>
          <el-descriptions-item label="入职日期">{{ currentEmployee.entryDate }}</el-descriptions-item>
          <el-descriptions-item label="转正日期">{{ currentEmployee.formalDate }}</el-descriptions-item>
          <el-descriptions-item label="转正薪资">{{ currentEmployee.salary }}</el-descriptions-item>
          <el-descriptions-item label="岗位职级">{{ currentEmployee.level }}</el-descriptions-item>
        </el-descriptions>

        <div class="formal-comments" v-if="currentEmployee.formal">
          <h4>转正评语</h4>
          <p>{{ currentEmployee.formal.comment }}</p>
          <h4>工作目标</h4>
          <p>{{ currentEmployee.formal.goals }}</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'pending',
      pendingList: [
        {
          id: 1,
          name: '张三',
          position: '前端开发工程师',
          department: '技术部',
          entryDate: '2024-01-01',
          probationResult: '通过'
        }
      ],
      completedList: [
        {
          id: 2,
          name: '李四',
          position: '后端开发工程师',
          department: '技术部',
          formalDate: '2024-03-15',
          salary: '18000',
          level: 'P2',
          formal: {
            comment: '试用期表现优秀，建议转正',
            goals: '1. 负责核心模块开发\n2. 指导新人成长\n3. 推动技术创新'
          }
        }
      ],
      formalDialogVisible: false,
      detailDialogVisible: false,
      currentEmployee: null,
      formalForm: {
        formalDate: '',
        salary: 0,
        level: '',
        comment: '',
        goals: ''
      }
    }
  },
  methods: {
    handleFormal(row) {
      this.currentEmployee = row
      this.formalForm = {
        formalDate: '',
        salary: 0,
        level: '',
        comment: '',
        goals: ''
      }
      this.formalDialogVisible = true
    },
    viewDetail(row) {
      this.currentEmployee = row
      this.detailDialogVisible = true
    },
    submitFormal() {
      if (!this.formalForm.formalDate || !this.formalForm.salary) {
        this.$message.error('请填写完整信息')
        return
      }

      // TODO: 提交转正信息到后端
      this.currentEmployee.formalDate = this.formatDate(this.formalForm.formalDate)
      this.currentEmployee.salary = this.formalForm.salary
      this.currentEmployee.level = this.formalForm.level
      this.currentEmployee.formal = {
        comment: this.formalForm.comment,
        goals: this.formalForm.goals
      }

      // 从待转正列表移动到已转正列表
      const index = this.pendingList.findIndex(item => item.id === this.currentEmployee.id)
      if (index > -1) {
        this.pendingList.splice(index, 1)
        this.completedList.unshift(this.currentEmployee)
      }

      this.$message.success('转正成功')
      this.formalDialogVisible = false
    },
    formatDate(date) {
      const d = new Date(date)
      return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
    }
  }
}
</script>

<style lang="scss" scoped>
.formal-appointment {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .formal-comments {
    margin-top: 20px;

    h4 {
      margin: 15px 0 10px;
      color: #303133;
    }

    p {
      margin: 0;
      color: #606266;
      line-height: 1.6;
      white-space: pre-line;
    }
  }
}
</style> 